package com.newshub.app.ui.category

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.newshub.app.data.model.News
import com.newshub.app.data.repository.NewsRepository
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.launch

class CategoryViewModel(
    private val newsRepository: NewsRepository
) : ViewModel() {
    
    private val compositeDisposable = CompositeDisposable()
    
    private val _newsList = MutableLiveData<List<News>>()
    val newsList: LiveData<List<News>> = _newsList
    
    private val _isLoading = MutableLiveData<Boolean>()
    val isLoading: LiveData<Boolean> = _isLoading
    
    private val _error = MutableLiveData<String>()
    val error: LiveData<String> = _error
    
    private val _selectedCategory = MutableLiveData<String>()
    val selectedCategory: LiveData<String> = _selectedCategory
    
    fun loadNewsByCategory(category: String, start: Int = 0, end: Int = 20) {
        _selectedCategory.value = category
        _isLoading.value = true
        
        val disposable = when (category) {
            "T1348647853363" -> newsRepository.getHeadlineNews(start, end)
            "T1467284926140" -> newsRepository.getFeaturedNews(start, end)
            "T1348648517839" -> newsRepository.getEntertainmentNews(start, end)
            "T1348649079062" -> newsRepository.getSportsNews(start, end)
            "5bmz6aG25bGx" -> newsRepository.getAutoNews(start, end)
            else -> newsRepository.getHeadlineNews(start, end)
        }
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                { news ->
                    _newsList.value = news
                    _isLoading.value = false
                },
                { throwable ->
                    _error.value = throwable.message ?: "加载失败"
                    _isLoading.value = false
                }
            )
        
        compositeDisposable.add(disposable)
    }
    
    fun refreshNews() {
        val category = _selectedCategory.value ?: "T1348647853363"
        loadNewsByCategory(category, 0, 20)
    }
    
    fun loadMoreNews() {
        val category = _selectedCategory.value ?: "T1348647853363"
        val currentList = _newsList.value ?: emptyList()
        val start = currentList.size
        val last = start + 20
        
        loadNewsByCategory(category, start, last)
    }
    
    fun toggleFavorite(news: News) {
        viewModelScope.launch {
            try {
                if (news.isFavorite) {
                    newsRepository.removeFromFavorite(news.docid)
                } else {
                    newsRepository.addToFavorite(news)
                }
                
                // 更新本地列表
                val currentList = _newsList.value ?: emptyList()
                val updatedList = currentList.map { item ->
                    if (item.docid == news.docid) {
                        item.copy(isFavorite = !item.isFavorite)
                    } else {
                        item
                    }
                }
                _newsList.value = updatedList
            } catch (e: Exception) {
                _error.value = e.message ?: "操作失败"
            }
        }
    }
    
    override fun onCleared() {
        super.onCleared()
        compositeDisposable.clear()
    }
}























